﻿using Newtonsoft.Json;
using System.Text;

namespace LowCoding.Common
{
    public class ToJsonTran
    {
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <returns></returns>
        public static ResponseMessageModel Success()
        {
            return Success(null);
        }

        public static ResponseMessageModel Success(object data)
        {
            return new ResponseMessageModel() { success = true, code = (int)ResponseCodeEnum.Success, message = "success", data = data };
        }

        //public static ResponseMessageModel<T> Success<T>(T data)
        //{
        //    return new ResponseMessageModel<T>() { success = true, code = (int)ResponseCodeEnum.Success, message = "success", data = data };
        //}

        public static ResponseMessageModel Success(string msg)
        {
            return new ResponseMessageModel() { success = true, code = (int)ResponseCodeEnum.Success, message = msg, data = null };
        }

        /// <summary>
        /// 根据bool类型返回相应结果
        /// </summary>
        /// <param name="isok"></param>
        /// <returns></returns>
        public static ResponseMessageModel ResultByBool(bool isok)
        {
            if (isok)
            {
                return Success();
            }
            else
            {
                return Fail("操作失败");
            }
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResponseMessageModel Fail(int code, string message)
        {
            return Fail(code, message, null);
        }

        public static ResponseMessageModel Fail(string message)
        {
            return Fail(ResponseCodeEnum.Fail, message, null);
        }

        public static ResponseMessageModel Fail(ResponseCodeEnum code, string message)
        {
            return Fail((int)code, message, null);
        }

        public static ResponseMessageModel Fail(ResponseCodeEnum code, string message, object data)
        {
            return new ResponseMessageModel() { success = false, code = (int)code, message = message, data = data };
        }

        public static ResponseMessageModel Fail(int code, string message, object data)
        {
            return new ResponseMessageModel() { success = false, code = code, message = message, data = data };
        }

        public static HttpResponseMessage ToJson(Object obj)
        {
            String str;
            if (obj is String || obj is Char)
            {
                str = obj.ToString();
            }
            else
            {
                str = JsonConvert.SerializeObject(obj);
            }
            //转换返回接口中的null为空字符串
            str = str.Replace("null", "\"\"");
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }
    }
}